package com.mtw.bbs.common.redisBase.config;


import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;

/**
 * redisson配置
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.data.redis.redisson", name = "dataId")
public class RedissonConfig {

    @Value("${spring.data.redis.redisson.dataId}")
    private String dataId;

    @Value("${spring.cloud.nacos.config.group}")
    private String group;

    @Autowired
    private NacosConfigManager nacosConfigManager;

    @Bean
    public RedissonClient redisson() throws IOException, NacosException {

        ConfigService configService =nacosConfigManager.getConfigService();
        String config = configService.getConfig(dataId, group, 3000);
        return Redisson.create(Config.fromYAML(config));

//        URL resource = RedissonConfig.class.getClassLoader().getResource("redisson.yml");
//        Config config = Config.fromYAML();
//        return Redisson.create(config);
    }


}
